﻿#pragma once

#include <iostream>
#include <vector>

#include <string>

using namespace std;

namespace open_address
{
	enum State
	{
		EXIST,
		EMPTY,
		DELETE
	};


	//当key是string / Date等类型时，key不能取模，那么我们需要给HashTable增加⼀个仿函数，这个仿函
	//数⽀持把key转换成⼀个可以取模的整形，如果key可以转换为整形并且不容易冲突，那么这个仿函数
	//就⽤默认参数即可，如果这个Key不能转换为整形，我们就需要⾃⼰实现⼀个仿函数传给这个参数，实
	//现这个仿函数的要求就是尽量key的每值都参与到计算中，让不同的key转换出的整形值不同。string
	//做哈希表的key⾮常常⻅，所以我们可以考虑把string特化
	template<class K>
	struct HashFunc
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	template<>
	struct HashFunc<string>
	{
		// 字符串转换成整形，可以把字符ascii码相加即可
		// 但是直接相加的话，类似"abcd"和"bcad"这样的字符串计算出是相同的
		// 这⾥我们使⽤BKDR哈希的思路，⽤上次的计算结果去乘以⼀个质数，这个质数⼀般去31, 131等效果会⽐较好
		size_t operator()(const string& s)
		{
			size_t hash = 0;

			for (auto& e : s)
			{
				hash *= 131;
				hash += e;
			}

			return hash;
		}
	};

	template<class K, class V>
	struct HashData
	{
		pair<K, V> _kv;
		State _state = EMPTY;

	};

	template<class K, class V, class Hash = HashFunc<K>>
	class HashTable
	{
	public:
		inline unsigned long __stl_next_prime(unsigned long n)
		{
			// 扩容质数表
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
			53, 97, 193, 389, 769,
			1543, 3079, 6151, 12289, 24593,
			49157, 98317, 196613, 393241, 786433,
			1572869, 3145739, 6291469, 12582917, 25165843,
			50331653, 100663319, 201326611, 402653189, 805306457,
			1610612741, 3221225473, 4294967291
			};
			const unsigned long* first = __stl_prime_list;
			const unsigned long* last = __stl_prime_list +
				__stl_num_primes;
			const unsigned long* pos = lower_bound(first, last, n);
			return pos == last ? *(last - 1) : *pos;
		}

		HashTable()
		{
			_table.resize(__stl_next_prime(0));
		}

		bool Insert(const pair<K, V>& kv)
		{
			if (Find(kv.first))
			{
				return false;
			}

			if (_n * 10 / _table.size() >= 7)
			{
				HashTable<K, V, Hash> newHT;

				newHT._table.resize(__stl_next_prime(_table.size() + 1));

				for (size_t i = 0; i < _table.size(); ++i)
				{
					if (_table[i]._state == EXIST)
					{
						newHT.Insert(_table[i]._kv);//复用Insert,因为这时候空间已经扩容了，复用时不可能走到扩容这里了
						//在新表中Insert注意，是将旧表中的值重新映射到新表，意味着可能在旧表冲突的值，在新表中可能就不冲突了
						//比如原来容量是11，13这个值就映射到2这个位置，假设新表扩容到22，13就重新映射到13，不在原来的位置
					}
				}

				_table.swap(newHT._table);
			}

			Hash hash;
			size_t hash0 = hash(kv.first) % _table.size();
			size_t hashi = hash0;
			size_t i = 1;

			while (_table[hashi]._state == EXIST)
			{
				hashi = (hash0 + i) % _table.size();
				++i;
			}

			_table[hashi]._kv = kv;
			_table[hashi]._state = EXIST;
			++_n;

			return true;

		}

		HashData<K, V>* Find(const K& key)
		{
			Hash hash;
			size_t hash0 = hash(key) % _table.size();
			size_t hashi = hash0;
			size_t i = 1;

			while (_table[hashi]._state != EMPTY)
			{
				if (_table[hashi]._state == EXIST && _table[hashi]._kv.first == key)
				{
					return &_table[hashi];
				}

				hashi = (hash0 + i) % _table.size();
				++i;
			}

			return nullptr;
		}

		bool Erase(const K& key)
		{
			HashData<K, V>* ret = Find(key);

			if (ret)
			{
				ret->_state == DELETE;
				--_n;
				return true;
			}
			else
			{
				return false;
			}
		}

	private:
		vector<HashData<K, V>> _table;
		size_t _n = 0;
	};
}

namespace hash_bucket
{
	template<class K,class V>
	struct HashNode
	{
		pair<K, V> _kv;
		HashNode<K, V>* _next;

		HashNode(const pair<K, V>& kv)
			:_kv(kv)
			, _next(nullptr)
		{

		}
	};

	template<class K>
	struct HashFunc
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& s)
		{
			size_t hash = 0;

			for (auto& e : s)
			{
				hash *= 131;
				hash += e;
			}

			return hash;
		}
	};

	template<class K,class V,class Hash=HashFunc<K>>
	class HashTable
	{
		typedef HashNode<K, V> Node;
	public:

		inline unsigned long __stl_next_prime(unsigned long n)
		{
			// 扩容质数表
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
			53, 97, 193, 389, 769,
			1543, 3079, 6151, 12289, 24593,
			49157, 98317, 196613, 393241, 786433,
			1572869, 3145739, 6291469, 12582917, 25165843,
			50331653, 100663319, 201326611, 402653189, 805306457,
			1610612741, 3221225473, 4294967291
			};
			const unsigned long* first = __stl_prime_list;
			const unsigned long* last = __stl_prime_list +
				__stl_num_primes;
			const unsigned long* pos = lower_bound(first, last, n);
			return pos == last ? *(last - 1) : *pos;
		}

		HashTable()
			:_table(__stl_next_prime(0))
			,_n(0)
		{

		}

		HashTable(const HashTable<K,V,Hash>& ht)
		{
			_table.reserve(ht._table.size());

			for (size_t i = 0; i < ht._table.size(); ++i)
			{
				Node* cur = ht._table[i];
				while (cur)
				{
					Insert(cur->_kv);
					cur = cur->_next;
				}
			}
		}

		void swap(HashTable<K, V,Hash>& ht)
		{
			std::swap(_table, ht._table);
			std::swap(_n, ht._n);
		}

		HashTable<K,V,Hash>& operator=(HashTable<K,V,Hash> ht)
		{
			swap(ht);
			return *this;
		}

		~HashTable()
		{
			for (size_t i = 0; i < _table.size(); ++i)
			{
				Node* cur = _table[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_table[i] = nullptr;
			}
		}

		bool Insert(const pair<K, V>& kv)
		{
			Hash hash;
			if (_n == _table.size())
			{
				vector<Node*> newTable(__stl_next_prime(_table.size() + 1),nullptr);

				for (size_t i = 0; i < _table.size(); ++i)
				{
					Node* cur = _table[i];
					while (cur)
					{
						Node* next = cur->_next;

						size_t hashi = hash(cur->_kv.first) % newTable.size();
						
						cur->_next = newTable[hashi];
						newTable[hashi] = cur;

						cur = next;

					}

					_table[i] = nullptr;
				}

				_table.swap(newTable);
			}

			size_t hashi = hash(kv.first) % _table.size();

			Node* newnode = new Node(kv);
			newnode->_next = _table[hashi];
			_table[hashi] = newnode;
			++_n;

			return true;
		}

		Node* Find(const K& key)
		{
			Hash hash;
			size_t hashi = hash(key) % _table.size();
			
			Node* cur = _table[hashi];
			while (cur)
			{
				if (cur->_kv.first == key)
				{
					return cur;
				}

				cur = cur->_next;
			}
			return nullptr;
		}

		bool Erase(const K& key)
		{
			Hash hash;
			size_t hashi = hash(key) % _table.size();
			Node* prev = nullptr;
			Node* cur = _table[hashi];

			while (cur)
			{
				if (cur->_kv.first == key)
				{
					if (prev == nullptr)
					{
						_table[hashi] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}

					delete cur;
					--_n;
					return true;
				}
				else
				{
					prev = cur;
					cur = cur->_next;
				}
			}
			return false;
		}

	private:
		vector<Node*> _table;
		size_t _n = 0;
	};
}

